package com.efuture.druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @ClassNAME DruidUtils
 * @Description Druid连接池工具类
 * @Author zhaoshanshan
 * @Date 2021/9/20 8:48 下午
 * @Version 1.0
 */
public class DruidUtils {
    private static DataSource ds;
    static {
       try{
           Properties pro = new Properties();
           pro.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
           ds =  DruidDataSourceFactory.createDataSource(pro);
       }catch (Exception e){
           e.printStackTrace();
       }
    }
    //获取数据库连接池
    public static DataSource getDataSource(){
        return ds;
    }
    //获取数据库连接对象
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static void close( Connection conn, Statement stmt){
        close(null,conn,stmt);
    }
    //资源释放
    public static void close(ResultSet rst, Connection conn, Statement stmt){
        if(null!=rst){
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=stmt){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}